package com.hmsm.sys.daoImpl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hmsm.server.base.BaseDao;
import com.hmsm.sys.dao.SysMessagePublishDao;
import com.hmsm.sys.po.SysMessagePublish;

@Repository
public class SysMessagePublishDaoImpl extends BaseDao<SysMessagePublish, Integer> implements SysMessagePublishDao {
	/**
	 * 
	 *  @Description:添加消息
	 *  @MethodName :getAllMessage
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-12 上午11:32:00 
	 *  @return List<SysMessagePublish>
	 *
	 */
	public List<SysMessagePublish> getMessageList(String title,String startTime,String endTime){
		String sql="SELECT * FROM sys_message_publish WHERE 1 = 1 ";
		StringBuffer sb=new StringBuffer();
		sb.append(sql);
		if(title!=null&&!"".equals(title)){
			sb.append(" AND title LIKE'%"+title+"%'");
		}
		if(startTime!=null&&!"".equals(startTime)){
			sb.append(" AND MESS_PUBLISH_DATE >= '"+startTime+"'");
		}
		if(endTime!=null&&!"".equals(endTime)){
			endTime = endTime + " 23:59:59";
			sb.append(" AND MESS_PUBLISH_DATE <= '"+endTime+"'");
		}
		sb.append(" ORDER BY MESS_PUBLISH_DATE DESC ;");
		return this.queryBySql(sb.toString());
	}
	
	/**
	 * 
	 *  @Description:查询最新消息
	 *  @MethodName :querryMessageTOP
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-12 下午03:26:38 
	 *  @return
	 *
	 */
	public SysMessagePublish querryMessageTOP() {
		String sql="SELECT * FROM sys_message_publish ORDER BY MESS_PUBLISH_DATE DESC LIMIT 0, 1;";
		List<SysMessagePublish> list = this.queryBySql(sql);
		if (list!=null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
	
	/**
	 * 
	 *  @Description: 分页查询消息列表
	 *  @MethodName :getMessageList
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-22 下午02:03:04 
	 *  @param beginTime
	 *  @param endTime
	 *  @param title
	 *  @param pageIndex
	 *  @param pageSize
	 *  @return
	 *
	 */
	@SuppressWarnings("unchecked")
	public List<SysMessagePublish> getMessageList(final Date beginTime,final Date endTime,final String title,final Integer pageIndex,final Integer pageSize){
		Object obj = getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cr = session.createCriteria(SysMessagePublish.class);
				if(beginTime != null){
					cr.add(Restrictions.ge("messPublishDate", beginTime));
				}
				if (endTime != null) {
					cr.add(Restrictions.le("messPublishDate", endTime));
				}
				if (title != null && !title.equals("")) {
					cr.add(Restrictions.like("title", title, MatchMode.ANYWHERE));
				}
				return cr.addOrder(Order.desc("messPublishDate")).setFirstResult((pageIndex-1)*pageSize).setMaxResults(pageSize).list();
			}
		});
		return (List<SysMessagePublish>) obj;
	}
	/**
	 * 
	 *  @Description:分页查询消息条数
	 *  @MethodName :getMessageCount
	 *  @Author     :zhangshuai
	 *  @CreateDate :2013-7-22 下午01:58:36 
	 *  @param beginTime
	 *  @param endTime
	 *  @param title
	 *  @return
	 *
	 */
	public int getMessageCount(final Date beginTime,final Date endTime,final String title){
		Object obj = getHibernateTemplate().execute(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria cr = session.createCriteria(SysMessagePublish.class);
				if(beginTime != null){
					cr.add(Restrictions.ge("messPublishDate", beginTime));
				}
				if (endTime != null) {
					cr.add(Restrictions.le("messPublishDate", endTime));
				}
				if (title != null && !title.equals("")) {
					cr.add(Restrictions.like("title", title, MatchMode.ANYWHERE));
				}
				return cr.setProjection(Projections.rowCount()).uniqueResult();
			}
		});
		if (obj != null) {
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}
}
